using System;
using System.Collections.Generic;
using Godot;

namespace Arcane.Source.Client
{
    public class DialogBuilder
    {
        private string title = string.Empty;
        private Control content;
        private readonly Dictionary<string, Action> _buttons = [];

        public DialogBuilder WithTitle(string title)
        {
            this.title = title;
            return this;
        }

        public DialogBuilder WithContent(Control content)
        {
            this.content = content;
            return this;
        }

        public DialogBuilder WithMessage(string message)
        {
            if (content is Label l)
            {
                l.Text += '\n' + message;
                return this;
            }

            var label = new Label
            {
                Text = message,
                VerticalAlignment = VerticalAlignment.Center,
                HorizontalAlignment = HorizontalAlignment.Center,
            };
            return WithContent(label);
        }

        public DialogBuilder AddButton(string text, Action onClick)
        {
            _buttons.Add(text, onClick);
            return this;
        }

        public DialogBuilder AddCloseButton(GameOS game) => AddButton("Close", game.HideAlert);

        public Control Build(Color panelColor)
        {
            var alert = new ColorRect
            {
                Color = panelColor,

                AnchorLeft = 0.3f,
                AnchorRight = 0.7f,
                AnchorTop = 0.3f,
                AnchorBottom = 0.7f,

                OffsetLeft = 0,
                OffsetRight = 0,
                OffsetTop = 0,
                OffsetBottom = 0,
            };

            if (content != null)
            {
                content.AnchorLeft = 0;
                content.AnchorRight = 1;
                content.AnchorBottom = 0.75f;

                if (!string.IsNullOrEmpty(title))
                {
                    var tt = new Label
                    {
                        Text = title,
                        AnchorLeft = 0,
                        AnchorRight = 1,
                        AnchorTop = 0,
                        AnchorBottom = 0.25f,

                        OffsetLeft = 0,
                        OffsetRight = 0,
                        OffsetBottom = 0,
                        OffsetTop = 0,

                        HorizontalAlignment = HorizontalAlignment.Center,
                        VerticalAlignment = VerticalAlignment.Center
                    };
                    alert.AddChild(tt);
                    content.AnchorTop = 0.25f;
                }
                else
                {
                    content.AnchorTop = 0;
                }

                content.OffsetLeft = content.OffsetRight = content.OffsetBottom = content.OffsetTop = 0;
                alert.AddChild(content);
            }

            var buttonGroup = new HBoxContainer
            {
                AnchorLeft = 0,
                AnchorRight = 1,
                AnchorTop = 0.75f,
                AnchorBottom = 1,

                OffsetLeft = 0,
                OffsetRight = 0,
                OffsetTop = 0,
                OffsetBottom = 0,
            };

            foreach (var (text, callback) in _buttons)
            {
                var button = new Button
                {
                    Text = text,
                    SizeFlagsHorizontal = Control.SizeFlags.ExpandFill
                };
                button.ButtonUp += callback;
                buttonGroup.AddChild(button);
            }
            alert.AddChild(buttonGroup);
            return alert;
        }

        public Control Build() => Build(new Color(0x333333ff));
    }
}